package com.ch.gulimall.product.service.impl;

import com.ch.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.ch.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ch.gulimall.product.service.AttrAttrgroupRelationService;
import com.ch.gulimall.product.vo.AttrVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.common.utils.PageUtils;
import com.ch.common.utils.Query;

import com.ch.gulimall.product.dao.AttrDao;
import com.ch.gulimall.product.entity.AttrEntity;
import com.ch.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationService attrgroupRelationService;

    @Autowired

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        //spring自带的工具类
        BeanUtils.copyProperties(attr, attrEntity);
        this.save(attrEntity);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
        attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
        attrgroupRelationService.save(attrAttrgroupRelationEntity);
    }

    @Override
    public PageUtils queryBasePage(Map<String, Object> map, Long catLogId) {
        //首先我们知道这是个base基本属性的get
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if(catLogId!=0){
            wrapper.eq("catelog_id", catLogId);
        }
        String key = (String) map.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and(obj->{
               obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(map), wrapper);
//        List<AttrEntity> records = page.getRecords();
//        List<AttrVo> attrVoList=new CopyOnWriteArrayList<>();
//        records.parallelStream().forEach(attrEntity -> {
//            AttrVo attrVo = new AttrVo();
//            BeanUtils.copyProperties(attrEntity,attrVo);
//            attrVoList.add(attrVo);
//        });
//        attrVoList.forEach(attrVo -> {
//            attrVo.setCatelogName();
//            attrVo.setGroupName();
//        });

        return new PageUtils(page);
    }

}
